package org.benben.shortlink.admin.remote;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.benben.shortlink.admin.common.convention.result.Result;
import org.benben.shortlink.admin.dto.req.ShortLinkGroupStatsReqDTO;
import org.benben.shortlink.admin.remote.dto.req.*;
import org.benben.shortlink.admin.remote.dto.resp.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 *
 * @author benben
 */
public interface ShortLinkRemoteService {

    /**
     * 分页查询短链接
     *
     * @param requestParam 短链接分页请求参数
     * @return 短链接分页响应
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", requestParam.getGid());
        requestMap.put("orderTag", requestParam.getOrderTag());
        requestMap.put("current", requestParam.getCurrent());
        requestMap.put("size", requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", requestMap);

        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {
            @Override
            public Result<IPage<ShortLinkPageRespDTO>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 创建短链接
     *
     * @param requestParam 短链接创建入参
     * @return 短链接创建响应
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam) {
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr, new TypeReference<Result<ShortLinkCreateRespDTO>>() {
            @Override
            public Result<ShortLinkCreateRespDTO> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 查询分组内短链接数量
     *
     * @param requestParam gid列表
     * @return 分组下短链接数量返回参数
     */
    default Result<List<ShortLinkGroupCountQueryRespDTO>> listShortLinkCount(List<String> requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("requestParam", requestParam);
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(result, new TypeReference<Result<List<ShortLinkGroupCountQueryRespDTO>>>() {
            @Override
            public Result<List<ShortLinkGroupCountQueryRespDTO>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 修改短链接
     *
     * @param requestParam 短链接修改入参
     */
    default void updateShortLink(ShortLinkUpdateReqDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/update", JSON.toJSONString(requestParam));
    }

    /**
     * 根据URL获取对应的标题
     *
     * @param url 目标网站路径
     * @return 目标网站标题
     */
    default Result<String> getTitleByUrl(String url) {
        String title = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url=" + url);
        return JSON.parseObject(title, new TypeReference<Result<String>>() {
            @Override
            public Result<String> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 保存回收站
     *
     * @param requestParam 请求参数
     * @return 无
     */
    default void saveRecycleBin(RecycleBinSaveReqDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
    }

    /**
     * 分页查询回收站短链接
     *
     * @param requestParam 请求参数
     * @return 回收站短链接分页响应
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList", requestParam.getGidList());
        requestMap.put("current", requestParam.getCurrent());
        requestMap.put("size", requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", requestMap);

        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {
            @Override
            public Result<IPage<ShortLinkPageRespDTO>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 恢复短链接
     *
     * @param requestParam 请求参数
     */
    default void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
    }

    /**
     * 从回收站删除短链接
     *
     * @param requestParam
     */
    default void removeRecycleBin(RecycleBinRemoveReqDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
    }

    /**
     * 访问单个短链接指定时间内监控数据
     *
     * @return 短链接监控信息
     */
    default Result<ShortLinkStatsRespDTO> oneShortLinkStats(ShortLinkStatsReqDTO requestParam) {
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats", BeanUtil.beanToMap(requestParam));
        return JSON.parseObject(resultPageStr, new TypeReference<Result<ShortLinkStatsRespDTO>>() {
            @Override
            public Result<ShortLinkStatsRespDTO> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 访问单个短链接指定时间内访问记录监控数据
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(requestParam, false, true);
        paramMap.remove("orders");
        paramMap.remove("records");
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats/access-record", paramMap);
        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkStatsAccessRecordRespDTO>>>() {
            @Override
            public Result<IPage<ShortLinkStatsAccessRecordRespDTO>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 获取分组短链接监控数据
     *
     * @param requestParam 获取分组短链接监控数据入参
     * @return 分组短链接监控数据
     */
    default Result<ShortLinkStatsRespDTO> groupShortLinkStats(ShortLinkGroupStatsReqDTO requestParam) {
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats/group", BeanUtil.beanToMap(requestParam));
        return JSON.parseObject(resultPageStr, new TypeReference<Result<ShortLinkStatsRespDTO>>() {
            @Override
            public Result<ShortLinkStatsRespDTO> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 访问分组短链接指定时间内访问记录监控数据
     *
     * @param requestParam 获取分组短链接监控访问记录数据入参
     * @return 访问记录监控数据
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> groupShortLinkStatsAccessRecord(ShortLinkGroupStatsAccessRecordReqDTO requestParam) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(requestParam, false, true);
        paramMap.remove("orders");
        paramMap.remove("records");
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats/access-record/group", paramMap);
        return JSON.parseObject(resultPageStr, new TypeReference<Result<IPage<ShortLinkStatsAccessRecordRespDTO>>>() {
            @Override
            public Result<IPage<ShortLinkStatsAccessRecordRespDTO>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

    /**
     * 批量创建短链接
     *
     * @param requestParam 批量创建短链接请求参数
     * @return 批量创建短链接返回参数
     */
    default Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(ShortLinkBatchCreateReqDTO requestParam) {
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create/batch", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr, new TypeReference<Result<ShortLinkBatchCreateRespDTO>>() {
            @Override
            public Result<ShortLinkBatchCreateRespDTO> parseObject(String text) {
                return super.parseObject(text);
            }
        });
    }

}
